package com.itheima.controller;
/*
    ヾ(❀ﾟ▽ﾟ)ノ 
 
 Grey1996year@163.com 
 
*/

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

	@Reference
	private MenuService menuService;

	@RequestMapping("/findMenu2Show")
	public Result findMenu2Show(){

		//获取登录的用户
		User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String username = user.getUsername();
		System.out.println("context-username----"+username);
		try {
			List<Map<String,Object>> list = menuService.findMenu2Show(username);

			return new Result(true,"",list);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}

	}

	@RequestMapping("/findPage")
	public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

		try {
			PageResult pageResult = menuService.findPage(queryPageBean);
			return pageResult;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}

	}
	@RequestMapping("/add")
	public Result add(@RequestBody Menu menu){

		try {
			menuService.add(menu);
			return new Result(true,"");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}
	}

	@RequestMapping("/findById")
	public Result findById(Integer id){

		try {
			Menu menu = menuService.findById(id);
			return new Result(true,"",menu);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}
	}
	@RequestMapping("/edit")
	public Result update(@RequestBody Menu menu){


		try {
			menuService.update(menu);
			return new Result(true,"");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}
	}
	@RequestMapping("/delete")
	public Result delete(Integer id){

		try {
			menuService.delete(id);
			return new Result(true,"");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}
	}
}
